package model;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;

import Mail.ImapServer;
import sun.tools.jar.resources.jar;

/**
 * Servlet implementation class Login
 */
@WebServlet("/login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String user = request.getParameter("username");
		String pwd = request.getParameter("password");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.println(user);
		out.println(pwd);
		// Security.addProvider(new
		// com.sun.net.ssl.internal.ssl.Provider());
		String host = "imap.163.com";
		int port = 993;
		String username = "syseye@163.com";
		String password = "i191908577";
		// final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

		IMAPFolder folder = null;
		IMAPStore store = null;
		try {
			ImapServer iServer = new ImapServer(host, port, username, password);
			store = iServer.Connect();

			Folder defaultFolder = store.getDefaultFolder();
			Folder[] allFolder = defaultFolder.list();
			for (int i = 0; i < allFolder.length; i++) {
				System.out.println("这个是服务器中的文件夹=" + allFolder[i].getFullName());
			}

			folder = (IMAPFolder) store.getFolder("INBOX"); // 收件箱
			folder.open(Folder.READ_WRITE); // 使用只读方式打开收件箱
			int size = folder.getMessageCount();
			System.out.println("这里是打印的条数==" + size);
			Message[] mess = folder.getMessages();
			// Message message = folder.getMessage(size);
			for (int i = 0; i < 5; i++) {
				String from = mess[i].getFrom()[0].toString();
				String subject = mess[i].getSubject();
				Date date = mess[i].getSentDate();
				Object content = mess[i].getContent();
				int num = mess[i].getMessageNumber();

				out.println(content);
				out.println("<p>" + "This is mail number:" + num + "</p>");
				System.out.println("From: " + from);
				System.out.println("Subject: " + subject);
				System.out.println("Date: " + date);
				System.out.println("Number" + mess[i].getMessageNumber());
			}
			/*
			 * String from = message.getFrom()[0].toString(); String
			 * subject = message.getSubject(); Date date =
			 * message.getSentDate();
			 */
			/*
			 * BufferedReader br = new BufferedReader(new
			 * InputStreamReader(System.in));
			 */
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		} finally {
			try {
				if (folder != null) {
					folder.close(false);
				}
				if (store != null) {
					store.close();
				}
			} catch (MessagingException e) {
				e.printStackTrace();
			}
		} // try
		System.out.println("接收完毕！");
	}// doPost
}